<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="确认"
  title="标本送检"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="sendInspectionButton"
  :confirm-loading="loadingMap.batchGenerateSendInspection"
  size="small"
)
  .collection
    .collection__left
      el-descriptions(column="2")
        el-descriptions-item(label="业务来源:")
          span(style="font-weight: 600;color: black") {{ props.selectionRow[0].source}}
        el-descriptions-item(label="样本类型:")
          span(style="font-weight: 600;color: black") {{ props.selectionRow[0].specimenTypeName}}
        el-descriptions-item(label="批次号:")
          span(style="font-weight: 600;color: black") {{ batchNumber  }}
        el-descriptions-item(label="样本数量:") {{ props.selectionRow.length }}
          span(style="font-weight: 600;color: black") 
      el-form
        el-form-item(label="标本外送")
          Checkbox(
            v-model="outward"
            :true-value="1"
            :false-value="0"
            )
        el-form-item(label="外部机构" v-if="outward==1")
          mt-select(
            v-bind="selectOrgProps"
            v-model="outwardOrgId"
            @select="onChange"
            label="选择机构"
          ) 
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { type SelectProps, useNotify, Checkbox } from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";

//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();

const appmodel = defineModel({
  default: false,
});
const batchNumber = ref("");
const outward = ref<0 | 1>(0);
const outwardOrgId = ref("");
const outwardOrgName = ref("");
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};
const onChange = (val: any) => {
  outwardOrgId.value = val.value;
  outwardOrgName.value = val.label;
};

/**
 * 设备选择下拉框属性
 */
const selectOrgProps = computed<SelectProps>(() => {
  const props: SelectProps = {
    loadMethod: () => $api.upms.org.getOrgList({ parentId: "-1" }) ?? [],
    separator: ",",
    placeholder: "请选择机构",
    multiple: false,
  };

  return props;
});

const { data, request, loadingMap } = useRequest({
  loadcollectionMethod: () => $api.dict.item.listAsOptions("collection_method"),
  batchGenerateSendInspection: (params: any) => {
    return $api.lis.specimen.batchGenerateSendInspection(params).then(() => {
      $notify.success("送检成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
  getSeriaNumberCode: (val: string) => {
    $api.bdm.customerEncodingRules
      .getCustomerEncodingRulesByCodeId(val)
      .then((res) => {
        batchNumber.value = res;
      });
  },
});

const sendInspectionButton = () => {
  // 外送需要选择送检机构
  if (outward.value === 1) {
    if (outwardOrgId.value == "") {
      $notify.error("请选择外送机构");
      return;
    }
  }

  request("batchGenerateSendInspection", {
    specimenList: props.selectionRow,
    outward: outward.value,
    batchNumber: batchNumber.value,
    outwardOrgId: outwardOrgId.value,
    outwardOrgName: outwardOrgName.value,
  });
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      outward.value = 0;
      outwardOrgId.value = "";
      outwardOrgName.value = "";
      request("getSeriaNumberCode", 30);
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.collection {
  height: 100%;
  display: flex;

  &__left {
    flex: 5;
  }

  &__right {
    margin-left: var(--mt-margin);
    margin-left: 100px;
    flex: 5;
  }
}
</style>
